package battleship;
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Random;
/**
 *
 * @author Shaun Pickford
 * This class will create and return a character array of 100 characters or null value
 * that indiciate if there is a ship in the array position or not.
 *
 * A = Aircraft Carrier = 5 hits
 * B = Battleship = 4 hits
 * C = Cruiser = 3 hits
 * S = Submarine = 3 hits
 * D = Destroyer = 2 hitS
 */
public class GameBoard{

    /* Create variables to store game files and grid size */
    File game = new File("Game1.txt");
    File game2 = new File("Game2.txt");
    final int GRID_SIZE = 100;

    /*Create variables for player information */
    char[] gameBoard = new char[GRID_SIZE];
    boolean[] shotRecord = new boolean[GRID_SIZE];
    Ship playerAircraftCarrier = new Ship("Aircraft Carrier", 5);
    Ship playerBattleship = new Ship("Battleship", 4);
    Ship playerCruiser = new Ship("Cruiser", 3);
    Ship playerSubmarine = new Ship("Submarine", 3);
    Ship playerDestroyer = new Ship("Destroyer", 2);

    /*Create variables for opponent information */
    char[] opponentGameBoard = new char[GRID_SIZE];
    boolean[] opponentShotRecord = new boolean[GRID_SIZE];
    Ship opponentAircraftCarrier;
    Ship opponentBattleship;
    Ship opponentCruiser;
    Ship opponentSubmarine;
    Ship opponentDestroyer;

    /*Other variables */
    ShipPlacement placementObj = new ShipPlacement();
    boolean boardCreated = false;
    int opponentPreviousShot = 0;
    int playerPreviousShot = 0;
    String opponentPreviousShotResult = "";
    String playerPreviousShotResult = "";


    /* No-arg constructor */
    public void GameBoard(){
    }

    /* This method creates the game board...should only be called once per game */
    public void createGameBoard(){

        if(!boardCreated)
        {

                /* Create blank game board with no ships to begin with */
                for(int f = 0; f < 100; f++)
                {
                    opponentGameBoard[f] = 'N';
                    gameBoard[f] = 'N';
                }

                /* Call methods to place AI ships */
                placeAircraftCarrier('o');
                placeBattleship('o');
                placeCruiser('o');
                placeSubmarine('o');
                placeDestroyer('o');

                /* Call method to place user ships */
                PlaceShips placeShips = new PlaceShips();
                placeShips.placeUserShips();


                boardCreated = true;
                //}//try

            /*catch (FileNotFoundException e){
                e.printStackTrace();
            }*/
        }//end if boardCreated = false
        else
        {
            JOptionPane.showMessageDialog(null, "Game Board Already Created!");
        }
    }

    public void placeAircraftCarrier(char who)
    {
        if(who == 'o')
        {
        /* Create ship */

            opponentAircraftCarrier = new Ship("Aircraft Carrier", 5);

        /* Randomly generate starting point for first ship */
                Random rand = new Random();
                int tempStartPoint = rand.nextInt(100);
                tempStartPoint = tempStartPoint + 1;
                //System.out.println("Start point is " + tempStartPoint);

                /* Temporary variables to hold endpoints and directions */
                int tempEndPoint = 0;
                String tempDirection = "";
                int[] possibleEndPoints = new int[4];
                String[] possibleDirections = new String[4];

        /* Place opponent Aircraft Carrier */

                /* Check to get possible endpoints and directions for the
                 * randomly generated start point */
                placementObj.checkShipPlacement(opponentAircraftCarrier, tempStartPoint, 'o');
                possibleEndPoints = placementObj.getPossibleEndPoints();
                possibleDirections = placementObj.getPossibleDirections();

                /* check to see if there are possible places to put the ship */
                if(possibleEndPoints[0] == -1 && possibleEndPoints[1] == -1 && possibleEndPoints[2] == -1 && possibleEndPoints[3] == -1)
                {
                    //System.out.println("Nowhere to place the ship!");
                    placeAircraftCarrier('o');
                }

                boolean canUse = false;
                while(!canUse)
                {
                    int choice = rand.nextInt(4);
                    //System.out.println("possibleEndPoints[" + choice + "] is " + possibleEndPoints[choice] + " and direction is " + possibleDirections[choice]);
                    if(possibleEndPoints[choice] != -1)
                    {
                        canUse = true;
                        tempEndPoint = possibleEndPoints[choice];
                        tempDirection = possibleDirections[choice];
                        placementObj.placeOpponentShips(tempDirection, tempStartPoint, tempEndPoint, opponentAircraftCarrier);
                        //System.out.println("Aircraft Carrier Placed!");
                    }
                }//while the choice is a valid one
        }//if opponent

        if(who == 'p')
        {

        }

    }

    public void placeBattleship(char who)
    {
        if(who == 'o')
        {

            /* Create Ship */
             opponentBattleship = new Ship("Battleship", 4);
        /* Randomly generate starting point for first ship */
                Random rand = new Random();
                int tempStartPoint = rand.nextInt(100);
                tempStartPoint = tempStartPoint + 1;
                //System.out.println("Start point is " + tempStartPoint);

                /* Temporary variables to hold endpoints and directions */
                int tempEndPoint = 0;
                String tempDirection = "";
                int[] possibleEndPoints = new int[4];
                String[] possibleDirections = new String[4];

        /* Place opponent Battleship */

                /* Check to get possible endpoints and directions for the
                 * randomly generated start point */
                placementObj.checkShipPlacement(opponentBattleship, tempStartPoint, 'o');
                //System.out.println("Ship length is "+ opponentBattleship.shipLength);
                possibleEndPoints = placementObj.getPossibleEndPoints();
                possibleDirections = placementObj.getPossibleDirections();

                /* check to see if there are possible places to put the ship */
                if(possibleEndPoints[0] == -1 && possibleEndPoints[1] == -1 && possibleEndPoints[2] == -1 && possibleEndPoints[3] == -1)
                {
                    //System.out.println("Nowhere to place the ship!");
                    placeBattleship('o');
                }
                boolean canUse = false;
                while(!canUse)
                {
                    int choice = rand.nextInt(4);
                    //System.out.println("choice is " + choice);
                    //System.out.println("possibleEndPoints[" + choice + "] is " + possibleEndPoints[choice] + " and direction is " + possibleDirections[choice]);
                    if(possibleEndPoints[choice] != -1)
                    {
                        canUse = true;
                        tempEndPoint = possibleEndPoints[choice];
                        tempDirection = possibleDirections[choice];
                        placementObj.placeOpponentShips(tempDirection, tempStartPoint, tempEndPoint, opponentBattleship);
                        //System.out.println("Battleship Placed!");
                    }
                }//while the choice is a valid one
        }//if opponent

        if(who == 'p')
        {

        }


    }

    public void placeCruiser(char who)
    {
        if(who == 'o')
        {

                /* Create Ship */
                opponentCruiser = new Ship("Cruiser", 3);
        /* Randomly generate starting point for first ship */
                Random rand = new Random();
                int tempStartPoint = rand.nextInt(100);
                tempStartPoint = tempStartPoint + 1;
                //System.out.println("Start point is " + tempStartPoint);

                /* Temporary variables to hold endpoints and directions */
                int tempEndPoint = 0;
                String tempDirection = "";
                int[] possibleEndPoints = new int[4];
                String[] possibleDirections = new String[4];

        /* Place opponent Carrier */

                /* Check to get possible endpoints and directions for the
                 * randomly generated start point */
                placementObj.checkShipPlacement(opponentCruiser, tempStartPoint, 'o');
                //System.out.println("Ship length is "+ opponentCruiser.shipLength);
                possibleEndPoints = placementObj.getPossibleEndPoints();
                possibleDirections = placementObj.getPossibleDirections();

                /* check to see if there are possible places to put the ship */
                if(possibleEndPoints[0] == -1 && possibleEndPoints[1] == -1 && possibleEndPoints[2] == -1 && possibleEndPoints[3] == -1)
                {
                    //System.out.println("Nowhere to place the ship!");
                    placeCruiser('o');
                }
                boolean canUse = false;
                while(!canUse)
                {
                    int choice = rand.nextInt(4);
                    //System.out.println("choice is " + choice);
                    //System.out.println("possibleEndPoints[" + choice + "] is " + possibleEndPoints[choice] + " and direction is " + possibleDirections[choice]);
                    if(possibleEndPoints[choice] != -1)
                    {
                        canUse = true;
                        tempEndPoint = possibleEndPoints[choice];
                        tempDirection = possibleDirections[choice];
                        placementObj.placeOpponentShips(tempDirection, tempStartPoint, tempEndPoint, opponentCruiser);
                        //System.out.println("Cruiser Placed!");
                    }
                }//while the choice is a valid one
        }//if opponent

        if(who == 'p')
        {

        }


    }

    public void placeSubmarine(char who)
    {
        if(who == 'o')
        {

            /* Create ship */
            opponentSubmarine = new Ship("Submarine", 3);
        /* Randomly generate starting point for first ship */
                Random rand = new Random();
                int tempStartPoint = rand.nextInt(100);
                tempStartPoint = tempStartPoint + 1;
                //System.out.println("Start point is " + tempStartPoint);

                /* Temporary variables to hold endpoints and directions */
                int tempEndPoint = 0;
                String tempDirection = "";
                int[] possibleEndPoints = new int[4];
                String[] possibleDirections = new String[4];

        /* Place opponent Submarine */

                /* Check to get possible endpoints and directions for the
                 * randomly generated start point */
                placementObj.checkShipPlacement(opponentSubmarine, tempStartPoint, 'o');
                //System.out.println("Ship length is "+ opponentSubmarine.shipLength);
                possibleEndPoints = placementObj.getPossibleEndPoints();
                possibleDirections = placementObj.getPossibleDirections();

                /* check to see if there are possible places to put the ship */
                if(possibleEndPoints[0] == -1 && possibleEndPoints[1] == -1 && possibleEndPoints[2] == -1 && possibleEndPoints[3] == -1)
                {
                    //System.out.println("Nowhere to place the ship!");
                    placeSubmarine('o');
                }
                boolean canUse = false;
                while(!canUse)
                {
                    int choice = rand.nextInt(4);
                    //System.out.println("choice is " + choice);
                    //System.out.println("possibleEndPoints[" + choice + "] is " + possibleEndPoints[choice] + " and direction is " + possibleDirections[choice]);
                    if(possibleEndPoints[choice] != -1)
                    {
                        canUse = true;
                        tempEndPoint = possibleEndPoints[choice];
                        tempDirection = possibleDirections[choice];
                        placementObj.placeOpponentShips(tempDirection, tempStartPoint, tempEndPoint, opponentSubmarine);
                        //System.out.println("Submarine placed!");
                    }
                }//while the choice is a valid one
        }//if opponent

        if(who == 'p')
        {

        }


    }

    public void placeDestroyer(char who)
    {
        if(who == 'o')
        {

            /* Create ship */
            opponentDestroyer = new Ship("Destroyer", 2);
        /* Randomly generate starting point for first ship */
                Random rand = new Random();
                int tempStartPoint = rand.nextInt(100);
                tempStartPoint = tempStartPoint + 1;
                //System.out.println("Start point is " + tempStartPoint);

                /* Temporary variables to hold endpoints and directions */
                int tempEndPoint = 0;
                String tempDirection = "";
                int[] possibleEndPoints = new int[4];
                String[] possibleDirections = new String[4];

        /* Place opponent Destroyer */

                /* Check to get possible endpoints and directions for the
                 * randomly generated start point */
                placementObj.checkShipPlacement(opponentDestroyer, tempStartPoint, 'o');
                //System.out.println("Ship length is "+ opponentDestroyer.shipLength);
                possibleEndPoints = placementObj.getPossibleEndPoints();
                possibleDirections = placementObj.getPossibleDirections();

                /* check to see if there are possible places to put the ship */
                if(possibleEndPoints[0] == -1 && possibleEndPoints[1] == -1 && possibleEndPoints[2] == -1 && possibleEndPoints[3] == -1)
                {
                    System.out.println("Nowhere to place the ship!");
                    placeDestroyer('o');
                }
                boolean canUse = false;
                while(!canUse)
                {
                    int choice = rand.nextInt(4);
                    //System.out.println("choice is " + choice);
                    //System.out.println("possibleEndPoints[" + choice + "] is " + possibleEndPoints[choice] + " and direction is " + possibleDirections[choice]);
                    if(possibleEndPoints[choice] != -1)
                    {
                        canUse = true;
                        tempEndPoint = possibleEndPoints[choice];
                        tempDirection = possibleDirections[choice];
                        placementObj.placeOpponentShips(tempDirection, tempStartPoint, tempEndPoint, opponentDestroyer);
                        //System.out.println("Destroyer Placed!");
                    }
                }//while the choice is a valid one
        }//if opponent

        if(who == 'p')
        {

        }


    }
    public char[] getPlayerGameBoard(){

        return gameBoard;
    }

    public char[] getOpponentGameBoard(){

        return opponentGameBoard;
    }

    public void showGameBoard(){

         System.out.println("     1   2   3   4   5   6   7   8   9   10  ");
        System.out.println("   ==========================================");
        //int index = 0;
        for(int index = 0; index < 100; index = index + 10)
        {
            String rowID = "";
            switch(index)
            {
                case 0: rowID = "A"; break;
                case 10: rowID = "B"; break;
                case 20: rowID = "C"; break;
                case 30: rowID = "D"; break;
                case 40: rowID = "E"; break;
                case 50: rowID = "F"; break;
                case 60: rowID = "G"; break;
                case 70: rowID = "H"; break;
                case 80: rowID = "I"; break;
                case 90: rowID = "J"; break;
                default: rowID = ""; break;
            }
            System.out.println(rowID + " " + "|| " + gameBoard[index] + " | " + gameBoard[index + 1] + " | " + gameBoard[index + 2] + " | " + gameBoard[index + 3] + " | " + gameBoard[index + 4] + " | " + gameBoard[index + 5] + " | " + gameBoard[index + 6] + " | " + gameBoard[index + 7] + " | " + gameBoard[index + 8] + " | " + gameBoard[index + 9] + " || ");
            System.out.println("  ===========================================");
        }
        System.out.println("=============================================");
    }

    public void showOpponentGameBoard() {

        /*int index = 0;
        while(index < 100){
            System.out.println("Position " + index + ": " + opponentGameBoard[index]);
            index++;
        }*/

        System.out.println("     1   2   3   4   5   6   7   8   9   10  ");
        System.out.println("   ==========================================");
        //int index = 0;
        for(int index = 0; index < 100; index = index + 10)
        {
            String rowID = "";
            switch(index)
            {
                case 0: rowID = "A"; break;
                case 10: rowID = "B"; break;
                case 20: rowID = "C"; break;
                case 30: rowID = "D"; break;
                case 40: rowID = "E"; break;
                case 50: rowID = "F"; break;
                case 60: rowID = "G"; break;
                case 70: rowID = "H"; break;
                case 80: rowID = "I"; break;
                case 90: rowID = "J"; break;
                default: rowID = ""; break;
            }
            System.out.println(rowID + " " + "|| " + opponentGameBoard[index] + " | " + opponentGameBoard[index + 1] + " | " + opponentGameBoard[index + 2] + " | " + opponentGameBoard[index + 3] + " | " + opponentGameBoard[index + 4] + " | " + opponentGameBoard[index + 5] + " | " + opponentGameBoard[index + 6] + " | " + opponentGameBoard[index + 7] + " | " + opponentGameBoard[index + 8] + " | " + opponentGameBoard[index + 9] + " || ");
            System.out.println("  ===========================================");
        }
        System.out.println("=============================================");
    }

    public String hitOrMiss(int coordinate, char who){

        if(who == 'p')
        {
        /* If the shot has not been attempted already */
        if (!shotRecord[coordinate - 1])
        {

        boolean hOrM = false;
        String ship = "";
        shotRecord[coordinate - 1] = true;

        if (opponentGameBoard[coordinate - 1] == 'N')
        {
            hOrM = false;
        }

        else if (opponentGameBoard[coordinate - 1] == 'D')
        {
            hOrM = true;
            ship = "Destroyer";

            /*Increase hit for ship and check to see if it has sunk */
            opponentDestroyer.addHit();
            if(opponentDestroyer.hits == opponentDestroyer.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Destroyer!");
                ship="Destroyer-sunk";
            }
        }

        else if (opponentGameBoard[coordinate - 1] == 'C')
        {
            hOrM = true;
            ship = "Cruiser";
            
            /*Increase hit for ship and check to see if it has sunk */
            opponentCruiser.addHit();
            if(opponentCruiser.hits == opponentCruiser.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Cruiser!");
                ship = "Cruiser-sunk";
            }
        }

        else if (opponentGameBoard[coordinate - 1] == 'S')
        {
            hOrM = true;
            ship = "Submarine";

            /*Increase hit for ship and check to see if it has sunk */
            opponentSubmarine.addHit();
            if(opponentSubmarine.hits == opponentSubmarine.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Submarine!");
                ship = "Submarine-sunk";
            }
        }

        else if (opponentGameBoard[coordinate - 1] == 'B')
        {
            hOrM = true;
            ship = "Battleship";

            /*Increase hit for ship and check to see if it has sunk */
            opponentBattleship.addHit();
            if(opponentBattleship.hits == opponentBattleship.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Battleship!");
                ship = "Battleship-sunk";
            }
        }

        else if (opponentGameBoard[coordinate - 1] == 'A')
        {
            hOrM = true;
            ship = "Aircraft Carrier";

            /*Increase hit for ship and check to see if it has sunk */
            opponentAircraftCarrier.addHit();
            if(opponentAircraftCarrier.hits == opponentAircraftCarrier.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Aircraft Carrier!");
                ship = "Aircraft Carrier-sunk";
            }
        }

        /* if hit */
        if (hOrM)
        {
           Main.playerShots++;
           return ship;
        }
        else
        {
           Main.playerShots++;
           return "miss";
        }

        }//if the shot hasnt been tried already

        
        /* if the shot has been tried already */
        else
        {
            JOptionPane.showMessageDialog(null, "You have already tried that coordinate!");
            return "tryAgain";
        }
        }//if players turn




        
        /* If opponent (AI) turn */
        else
        {
        if (!opponentShotRecord[coordinate - 1])
        {

        boolean hOrM = false;
        String ship = "";
        opponentShotRecord[coordinate - 1] = true;

        if (gameBoard[coordinate - 1] == 'N')
        {
            hOrM = false;
        }

        else if (gameBoard[coordinate - 1] == 'D')
        {
            hOrM = true;
            ship = "Destroyer";

            /*Increase hit for ship and check to see if it has sunk */
            playerDestroyer.addHit();
            if(playerDestroyer.hits == playerDestroyer.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Destroyer!");
                ship="Destroyer-sunk";
            }
        }

        else if (gameBoard[coordinate - 1] == 'C')
        {
            hOrM = true;
            ship = "Cruiser";

            /*Increase hit for ship and check to see if it has sunk */
            playerCruiser.addHit();
            if(playerCruiser.hits == playerCruiser.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Cruiser!");
                ship = "Cruiser-sunk";
            }
        }

        else if (gameBoard[coordinate - 1] == 'S')
        {
            hOrM = true;
            ship = "Submarine";

            /*Increase hit for ship and check to see if it has sunk */
            playerSubmarine.addHit();
            if(playerSubmarine.hits == playerSubmarine.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Submarine!");
                ship = "Submarine-sunk";
            }
        }

        else if (gameBoard[coordinate - 1] == 'B')
        {
            hOrM = true;
            ship = "Battleship";

            /*Increase hit for ship and check to see if it has sunk */
            playerBattleship.addHit();
            if(playerBattleship.hits == playerBattleship.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Battleship!");
                ship = "Battleship-sunk";
            }
        }

        else if (gameBoard[coordinate - 1] == 'A')
        {
            hOrM = true;
            ship = "Aircraft Carrier";

            /*Increase hit for ship and check to see if it has sunk */
            playerAircraftCarrier.addHit();
            if(playerAircraftCarrier.hits == playerAircraftCarrier.shipLength)
            {
                //JOptionPane.showMessageDialog(null, "You Sunk Their Aircraft Carrier!");
                ship = "Aircraft Carrier-sunk";
            }
        }


        /* If hit */
        if (hOrM)
        {
           Main.opponentShots++;
           return ship;
        }
        else
        {
           Main.opponentShots++;
           return "miss";
        }
        }//if the shot hasnt been tried already



        /*If shot HAS been tried already */
        else
        {
            return "tryAgain";
        }
        }
    }

    public boolean[] getShotRecord(){

        return shotRecord;

    }

    public boolean[] getOpponentShotRecord(){

        return opponentShotRecord;
    }

    }

